package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * 收消息 创建类，实现MsgHandler接口
 * 类名上加上component和Topic(主题名称)
 */
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
@Slf4j
public class TaskCompleteContractMsgHandler implements MsgHandler {
    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
       log.info("收到主题消息"+jsonMsg);
        //类型装换
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);

        //根据消息更改售卖机的状态：
        //如果是投放工单：将售货机改为运营状态
        if (taskCompleteContract.getTaskType()== VMSystem.TASK_TYPE_DEPLOY){
            //修改为运营状态
            vendingMachineService.updateStatus(taskCompleteContract.getInnerCode(),VMSystem.VM_STATUS_RUNNING);
        }
        //如果是撤机：将售货机改为撤机状态
        if (taskCompleteContract.getTaskType()== VMSystem.TASK_TYPE_REVOKE){
            //修改为运营状态
            vendingMachineService.updateStatus(taskCompleteContract.getInnerCode(),VMSystem.VM_STATUS_REVOKE);
        }



    }
}
